# coding=utf-8
# 代码文件：chapter7/ch7.1.5.py

from docx import Document
from docx.oxml.ns import qn
from docx.shared import Inches
from docx.shared import Pt

document = Document()

style = document.styles['Normal']       # 获得默认样式
style.font.name = 'Times New Roman'     # 设置西文字体

style.font.size = Pt(12)

# 设置中文字体
style.element.rPr.rFonts.set(qn('w:eastAsia'), '幼圆')

records = [
    ('西城', 201991),
    ('海淀', 129508),
    ('东城', 130969),
    ('朝阳', 119170),
    ('丰台', 79933),
]

# 添加段落
p = document.add_paragraph('第45周北京新房成交排名 TOP20热盘中限竞房占近半')

# 添加图片
document.add_picture('data/北京各城区最高房价柱状图.png', width=Inches(6.0))

# 添加表格
table = document.add_table(rows=1, cols=2, style='Medium Grid 1 Accent 5')
hdr_cells = table.rows[0].cells
hdr_cells[0].text = '城区'
hdr_cells[1].text = '单价（元）'

for addr_dist, price in records:
    row_cells = table.add_row().cells
    row_cells[0].text = addr_dist
    row_cells[1].text = str(price)
# 保存文件
document.save(r'data/temp.docx')

print("保存文档成功.")
